package com.eascs.admin.o2o.loanAccount.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.common.dao.ProxyMongoDao;
import com.eascs.admin.o2o.loanAccount.dao.AccountLogDao;
import com.eascs.admin.o2o.loanAccount.entity.AccountLog;
import com.eascs.admin.o2o.loanAccount.entity.AccountLogForm;
import com.eascs.admin.o2o.loanAccount.service.IAccountLogService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.mongo.MongoPageQuery;
import com.eascs.common.util.ObjectArrayUtil;
import com.eascs.common.util.StringUtil;

/**
 * @author bao.pan 2017年11月28日 下午6:03:21
 */
@Service
public class AccountLogImpl implements IAccountLogService {
	@Autowired
	AccountLogDao accountLogDao;
	@Autowired
	ProxyMongoDao mogonDao;

	@Override
	public int add(AccountLog accountLog) {
		mogonDao.insert(accountLog);
		return 1;
	}

	/**
	 * @Description: <p>
	 *               分页查询帐务系统接口日志信息
	 *               </p>
	 * @author LMX
	 * @date 2017年12月19日 下午2:52:18
	 * @version V1.0
	 */
	public PageResult<Object[]> findAccountLogByPage(AccountLogForm accountLogForm) {
		MongoPageQuery<AccountLog> sqlPageQuery = new MongoPageQuery<AccountLog>(AccountLog.class, accountLogForm.getPageSize(), accountLogForm.getPageIndex());
		sqlPageQuery.setCollectionName("account_log");// setQuerySql("db.getCollection('').find({})");
		if (StringUtil.isNotBlank(accountLogForm.getLogType())) {
			sqlPageQuery.addParam("log_type", accountLogForm.getLogType());
		}
		if (StringUtil.isNotBlank(accountLogForm.getStatus())) {
			sqlPageQuery.addParam("status", accountLogForm.getStatus());
		}
		if (StringUtil.isNotBlank(accountLogForm.getLoanapplyNo())) {
			sqlPageQuery.addParam("loanapply_no", accountLogForm.getLoanapplyNo());
		}
//		if (StringUtil.isNotBlank(accountLogForm.getStartTime())) {
//			sqlPageQuery.append(" AND loan.loan_date >= ?");
//			sqlPageQuery.addParam(accountLogForm.getStartTime());
//		}
//		if (!StringUtil.isEmpty(accountLogForm.getEndTime())) {
//			sqlPageQuery.append(" AND loan.loan_date < ?");
//			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(accountLogForm.getEndTime()), 1)));
//		}
		sqlPageQuery.addSort("start_time", -1);
		PageResult<AccountLog> pageQuery = mogonDao.findDataWithPage(sqlPageQuery);
		PageResult<Object[]> pageResultObjecs = new PageResult<Object[]>(pageQuery.getPageSize(), pageQuery.getPageIndex(), pageQuery.getDbRowCount());
		List<Object> objs = new ArrayList<Object>();
		if (null != pageQuery && pageQuery.getDbRowCount() > 0) {
			for (Object object : pageQuery.getData()) {
				objs.add(object);
			}
		}
		pageResultObjecs.setData(ObjectArrayUtil.buildObjectArrayList(objs, new String[] { "id", "loanapplyNo", "logType", "requestid", "classname", "methodname", "status", "startTime", "endTime", "requestPs", "responsePs" }));
		return pageResultObjecs;
		// return accountLogDao.findAccountLogByPage(accountLogForm);
	}
	

	/**@Description:
	 *               <p>
	 *               根据loanapplyNo查询 帐务系统接口日志 详情
	 *               </p>
	 * @author LMX
	 * @date 2018年1月30日 下午2:32:09
	 * @version V1.0
	 * @param loanapplyNo
	 * @return
	 */
	public AccountLog fingLogByLoanapplyNo(String id) {
		return mogonDao.findOne(AccountLog.class, id);
	}

}
